package Queue;

public class MyQueue {
     static class ListNode {
         private int val;
         private ListNode prev;
         private ListNode next;

         public ListNode(int val) {
             this.val = val;
         }
     }

     private ListNode front;//队头
     private ListNode rear;//队尾
     private int usedSize;

    public void offer(int x) {//插入数据。队列的话相当于一个尾插法
        ListNode node = new ListNode(x);
        if(rear == null) {//如果是一个空队列那插入的元素就是队尾和队头
            rear = front = node;
        } else {//不是空队列
            node.prev = rear;
            rear.next = node;
            rear = node;
            node.next = null;
        }
        usedSize++;
    }

    public int poll() {//打印最前面的节点，并删除
        if(front == null) {
            return -1;
        }
        int ret = front.val;
        //删除头节点
        if(front == rear) {//只有一个节点,全部置为null
            front = rear = null;
        } else {
            front = front.next;
            front.prev = null;
        }
        usedSize--;
        return ret;
    }

    public int peek() {//获取队头的元素
        if(front == null) {
            return -1;
        }
        int ret = front.val;
        return ret;
    }

    public int getUsedSize() {//队列的长度
        return usedSize;
    }

    public boolean isEmpty() {//队列是否是空的
        return usedSize == 0;
    }

    //设计一个循环队列

}
